<script>
  import config from './config';
  import store from '@/store';
  import {
    getToken,
  } from '@/utils/auth';
  import {
    linkToLoginByWx,
    getUrlParam,
  } from './utils/common.js';
  import {
    getUserInfo,
  } from './api/user.js';
  
  export default {
    onLaunch: function () {
      this.initApp();
    },
    methods: {
      // 初始化应用
      initApp() {
        // 初始化应用配置
        this.initConfig();
        // 检查用户登录状态
        //#ifdef H5
        this.checkLogin();
        //#endif
        this.globalData.openId = uni.getStorageSync('openId') || '';
      },
      initWx() {
        console.log(wx);
      },
      initConfig() {
        this.globalData.config = config;
      },
      checkLogin() {
        const urlParam = getUrlParam(location.hash);
        const token = getToken();
        if (!token && !urlParam.unCheckToken) {
          linkToLoginByWx();
          // this.$tab.reLaunch('/pages/login')
          return;
        }
        if (token) {
          this.getUserInfo();
          
        }
      },
      async getUserInfo() {
        const res = await getUserInfo();
        if (res.code === 200) {
          res.token = getToken();
          this.$store.dispatch('setUserInfo', res);
        }
      },
    },
  };
</script>

<style lang="scss">
  @import '@/static/scss/index.scss'
</style>
